package com.health.work.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.health.work.R;
import com.health.work.adapter.QuestionListAdapter;
import com.health.work.model.QuestionBean;
import com.health.work.utils.DefaultJSONData;
import com.health.work.utils.HEALTHPASSPORT;
import com.health.work.utils.RequestParamtersUtil;
import com.health.work.utils.TipsUtil;
import com.health.work.utils.UserInfoUtils;

/**
 * 健康咨询页面
 * 
 * @author star
 * 
 */
public class HealthInquiryActivity extends Activity {

	public static ArrayList<QuestionBean> questionList;

	private Button back;
	private TextView doctor;
	private TextView expert;
	private TextView ask;
	private TextView askList;
	// true为专家，false为医生 0 false 1 true
	private String isExpert = "0";
	private final static int SUCCESS = 0;
	private final static int FAILE = 1;
	private final static int ERROR = 2;
	private static final int NOASK = 3;
	private static final int SHOWQUESTION = 4;
	private Thread thread;
	protected String errorMessage = "";
	private int ProfessionalCategoryId;
	protected QuestionListAdapter adapter;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUCCESS:
				TipsUtil.showAlert(HealthInquiryActivity.this, null, "数据请求成功！");
				break;
			case FAILE:
				TipsUtil.showAlert(HealthInquiryActivity.this, null, "数据请求失败！");
				break;
			case ERROR:
				TipsUtil.showAlert(HealthInquiryActivity.this, null, "未知错误！");
				break;
			case NOASK:
				TipsUtil.showAlert(HealthInquiryActivity.this, null,
						errorMessage);
				break;
			case SHOWQUESTION:
				// 显示 questionList 的数据 需要用到listview and adpter
				adapter = new QuestionListAdapter(HealthInquiryActivity.this,
						questionList, order_list);
				order_list.setAdapter(adapter);
				break;
			default:
				break;
			}

		}

	};

	private ListView order_list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.health_inquiry_layout);
		initPages();
		// getData();
	}

	private void initPages() {

		back = (Button) findViewById(R.id.btnBack);
		doctor = (TextView) findViewById(R.id.doctor);
		expert = (TextView) findViewById(R.id.expert);
		ask = (TextView) findViewById(R.id.ask);
		askList = (TextView) findViewById(R.id.ask_list);
		order_list = (ListView) findViewById(R.id.order_list);
		back.setOnClickListener(onClickListener);
		doctor.setOnClickListener(onClickListener);
		expert.setOnClickListener(onClickListener);
		ask.setOnClickListener(onClickListener);
		askList.setOnClickListener(onClickListener);

		order_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				Intent i = new Intent(HealthInquiryActivity.this,
						ProblemDetailsActivity.class);
				i.putExtra("id", questionList.get(pos).Id);
				i.putExtra("postion", pos);
				startActivity(i);
			}
		});
		 getData();
//		getDatas();
	}

	OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent;
			switch (v.getId()) {
			case R.id.btnBack:
				finish();
				break;
			case R.id.doctor:
				doctor.setBackgroundResource(R.drawable.order_left_selected);
				expert.setBackgroundResource(R.drawable.order_right_normal);
				// getGetTrusteeship();
				isExpert = "0";
				break;
			case R.id.expert:
				doctor.setBackgroundResource(R.drawable.order_left_normal);
				expert.setBackgroundResource(R.drawable.order_right_selected);
				isExpert = "1";
				break;
			case R.id.ask:
				getIsQuestion();
				break;
			case R.id.ask_list:
				intent = new Intent(HealthInquiryActivity.this,
						ProblemsListActivity.class);
				intent.putExtra("isExpert", isExpert);

				startActivity(intent);
				break;
			default:
				break;
			}
		}
	};
	private Thread thread2;
	private Thread thread3;

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return super.onKeyDown(keyCode, event);
	}

	private void getIsQuestion() {// 判断是否可以提问
		final String tel = UserInfoUtils.getUserProperty(this,
				HEALTHPASSPORT.MOBILEPHONE);
		thread2 = new Thread(new Runnable() {
			public void run() {
				new RequestParamtersUtil().IsQuestions(tel,
						new DefaultJSONData() {

							@Override
							public void parse(JSONObject object) {
								Log.e("IsQuestions ob", object.toString());
							}

							@Override
							public void parse(JSONArray array) {
								Log.e("IsQuestions arr", array.toString());
							}

							@Override
							public void parse(String result)
									throws JSONException {
								Log.e("IsQuestions str", result);
								if ("0".equals(result)) {
									Intent intent = new Intent(
											HealthInquiryActivity.this,
											AskActivity.class);
									intent.putExtra("isExpert", isExpert);
									startActivity(intent);
								} else if ("1".equals(result)) {
									errorMessage = "不能提问，上一个问题未结束！";
									handler.sendEmptyMessage(NOASK);

								} else if ("2".equals(result)) {
									errorMessage = "不能提问，今日已经提了三个问题";
									handler.sendEmptyMessage(NOASK);

								} else {
									handler.sendEmptyMessage(ERROR);

								}
							}
						});
			}
		});
		thread2.start();
	}

	
	/**
	 * 查询托管人
	 */
	private void getGetTrusteeship() {// 接口ok 已经ok
		final String tel = UserInfoUtils.getUserProperty(this,
				HEALTHPASSPORT.MOBILEPHONE);
		thread3 = new Thread(new Runnable() {
			public void run() {
				new RequestParamtersUtil().GetTrusteeship(tel,
						new DefaultJSONData() {

							@Override
							public void parse(JSONObject object) {
								Log.e("GetTrusteeship ob", object.toString());
							}

							@Override
							public void parse(JSONArray array) {
								Log.e("GetTrusteeship arr", array.toString());
							}

							@Override
							public void parse(String result)
									throws JSONException {
								Log.e("GetTrusteeship str", result);
								// if ("true".equals(result)) {
								// handler.sendEmptyMessage(SUCCESS);
								// } else if("false".equals(result)){
								// handler.sendEmptyMessage(FAILE);
								//
								// }else{
								// handler.sendEmptyMessage(ERROR);
								//
								// }
							}
						});
			}
		});
		thread3.start();
	}

	/**
	 * 获取问题列表数据
	 */
	private void getData() {
		final String mobilePhone = UserInfoUtils.getUserProperty(this,
				HEALTHPASSPORT.MOBILEPHONE);
		thread = new Thread(new Runnable() {
			public void run() {
				new RequestParamtersUtil().GetQuestions(mobilePhone,
						new DefaultJSONData() {

							@Override
							public void parse(String result)
									throws JSONException {
								if ("true".equals(result)) {
									handler.sendEmptyMessage(SUCCESS);
								} else if ("false".equals(result)) {
									handler.sendEmptyMessage(FAILE);
								} else if ("0".equals(result)) {
									errorMessage = "暂无数据！";
									handler.sendEmptyMessage(NOASK);
								} else {
									handler.sendEmptyMessage(ERROR);
								}
							}

							@Override
							public void parse(JSONObject object)
									throws JSONException {
								Log.e("GetQuestions", object.toString());
							}

							@Override
							public void parse(JSONArray array)
									throws JSONException {
								Log.e("GetQuestions", array.toString());
								if (array != null && array.length() > 0) {
									questionList = new ArrayList<QuestionBean>();
									for (int i = 0; i < array.length(); i++) {
										QuestionBean bean = new QuestionBean();
										bean.Evaluation = array
												.getJSONObject(i).getString(
														"Evaluation");
										bean.EvaluationState = array
												.getJSONObject(i).getString(
														"EvaluationState");
										bean.HealthyCareMobilephone = array
												.getJSONObject(i)
												.getString(
														"HealthyCareMobilephone");
										bean.Id = array.getJSONObject(i)
												.getInt("Id");
										bean.MobilePhone = array.getJSONObject(
												i).getString("MobilePhone");
										bean.ProfessionalCategoryId = array
												.getJSONObject(i)
												.getInt("ProfessionalCategoryId");
										bean.Question = array.getJSONObject(i)
												.getString("Question");
										// bean.QuestionTime=array.getJSONObject(i).getLong("QuestionTime");
										bean.State = array.getJSONObject(i)
												.getString("State");
										System.out.println(bean.Id);
										questionList.add(bean);
									}
									handler.sendEmptyMessage(SHOWQUESTION);
								}
							}
						});
			}
		});
		thread.start();
	}

	private void getDatas() {

		thread = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				new RequestParamtersUtil().GetQuestionsByPCategoryId(
						ProfessionalCategoryId, new DefaultJSONData() {

							@Override
							public void parse(String result)
									throws JSONException {
								// TODO Auto-generated method stub
							}

							@Override
							public void parse(JSONObject object)
									throws JSONException {
								// TODO Auto-generated method stub

							}

							@Override
							public void parse(JSONArray array)
									throws JSONException {
								// TODO Auto-generated method stub
								if (array.toString() != null) {
									questionList = new ArrayList<QuestionBean>();
									for (int i = 0; i < array.length(); i++) {
										QuestionBean bean = new QuestionBean();
										bean.Evaluation = array
												.getJSONObject(i).getString(
														"Evaluation");
										bean.EvaluationState = array
												.getJSONObject(i).getString(
														"EvaluationState");
										bean.HealthyCareMobilephone = array
												.getJSONObject(i)
												.getString(
														"HealthyCareMobilephone");
										bean.Id = array.getJSONObject(i)
												.getInt("Id");
										bean.MobilePhone = array.getJSONObject(
												i).getString("MobilePhone");
										bean.ProfessionalCategoryId = array
												.getJSONObject(i)
												.getInt("ProfessionalCategoryId");
										bean.Question = array.getJSONObject(i)
												.getString("Question");
										// bean.QuestionTime=array.getJSONObject(i).getLong("QuestionTime");
										bean.State = array.getJSONObject(i)
												.getString("State");
										System.out.println(bean.Id);
										questionList.add(bean);
									}
									handler.sendEmptyMessage(SHOWQUESTION);
								} else if (array.toString().equals("0")) {

								} else {
								}
							}
						});
			}
		});
		thread.start();
	}
}
